class Post < ActiveRecord::Base
  belongs_to :topic
  belongs_to :user
  has_many :reports
  
  validates_presence_of :content
  alias_method :associated_user, :user
  
  def owned_by?(user)
    self.user == user
  end
  
  def moderated_by?(user)
    topic.moderated_by? user
  end
  
  def moderatorship_for_user(user)
    topic.moderatorship_for_user user
  end
  
  def locked?
    topic.locked?
  end
  
  def private?
    topic.private?
  end
  
  def user
    @user ||= associated_user || Guest.new
  end
  
  def link # REFACTORME does this really belong in the model?
    "#{topic.link}#post_#{self.id}"
  end
  
  def title
    "RE: #{topic.name} - by #{user.name}"
  end
  
  def position
    count = Post.count(:conditions => ["topic_id = ? AND created_at < ? and id <> ?", self.topic_id, self.created_at, self.id])
    return count + 1
  end

end
